package cn.yuhe.org.controller;


import cn.yuhe.basic.annotation.PreAuthorize;
import cn.yuhe.basic.utils.AjaxResult;
import cn.yuhe.basic.utils.PageList;
import cn.yuhe.org.domain.Employee;
import cn.yuhe.org.query.EmployeeQuery;
import cn.yuhe.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @Description: 部门的接口类
* @Author: Mr.She
* @Version: 1.0
* @Date:  2022/6/24 10:10
*/
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    /**
    * @Title: loadById
    * @Description: 根据Id查询
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/6/24 9:35
    * @Parameters: [id]
    * @Return cn.itsource.org.domain.Employee
    */
    @GetMapping("/{id}") // 请求地址：http://localhost:8080/employee/1 GET请求
    @PreAuthorize(name = "查询单个员工",sn = "employee:loadById")
    public Employee loadById(@PathVariable("id") Long id){
        return employeeService.loadById(id);
    }

    /**
    * @Title: loadAll
    * @Description: 查询所有
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/6/24 9:35
    * @Parameters: []
    * @Return java.util.List<cn.itsource.org.domain.Employee>
    */
    @GetMapping// 请求地址：http://localhost:8080/employee GET请求
    @PreAuthorize(name = "查询所有员工",sn = "employee:loadAll")
    public List<Employee> loadAll(){
        return employeeService.loadAll();
    }

    /**
    * @Title: remove
    * @Description: 根据Id删除
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/6/24 9:39
    * @Parameters: [id]
    * @Return cn.itsource.basic.utils.AjaxResult
    */
    @DeleteMapping("/{id}") // 请求地址：http://localhost:8080/employee/1 GET请求
    @PreAuthorize(name = "删除员工",sn = "employee:remove")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            employeeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }


    /**
    * @Title: addOrUpt
    * @Description: 新增/修改
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/6/24 9:49
    * @Parameters: [employee]
    * @Return cn.itsource.basic.utils.AjaxResult
    */
    @PutMapping
    @PreAuthorize(name = "新增/修改员工",sn = "employee:addOrUpt")
    public AjaxResult addOrUpt(@RequestBody Employee employee){
        try {
            if(employee.getId() == null){
                employeeService.save(employee);
            }else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }


    /**
    * @Title: queryData
    * @Description: 分页+高级查询
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/6/24 9:51
    * @Parameters: [employeeQuery]
    * @Return cn.itsource.basic.utils.AjaxResult
    */
    @PostMapping
    @PreAuthorize(name = "分页&高级查询员工",sn = "employee:queryData")
    public PageList<Employee> queryData(@RequestBody EmployeeQuery employeeQuery){
        PageList<Employee> data = employeeService.queryData(employeeQuery);
        return data;
    }


    /**
    * @Title: batchDel
    * @Description: 批量删除
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/6/25 9:47
    * @Parameters: [ids]
    * @Return cn.itsource.basic.utils.AjaxResult
    */
    @PatchMapping
    @PreAuthorize(name = "批量删除员工",sn = "employee:batchDel")
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            employeeService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

}
